<?php 
	error_reporting(E_ALL ^E_DEPRECATED);//错误机制处理

	Class sql_class{
		public $prefix;//表前缀
		public $linkID;//连接ID
		public $table;//表名
		public $db_config;//配置内容
		public $sql;//sql语句

		/*public function createData($database,$charset){//建库
			$this->sql = "create database $database charset $charset";
			$query = mysql_query($this->sql);
			if($query){
				$this->S_log($this->sql);
			}else{
				$this->S_log($this->sql,mysql_error());
			}
			return $this;
		}*/

		public function createTable($table,$data){//创建表 $table新表名，$data字段
			$str = "";
			foreach($data as $k=>$v){
				$str .= "$k $v".",";
			}
			$str = rtrim($str,",");

			$this->sql="create table $table($str)";
			$query = mysql_query($this->sql);
			if($query){
				$this->S_log($this->sql);
			}else{
				$this->S_log($this->sql,mysql_error());
			}
			return $this;
		}

		function __construct($table,$config_path = "sql_config.php"){//用构造函数接收从test文件中传过来的值，就是表名。设置一个
			if(!empty($config_path)){//
				$this->db_config = require($config_path);//拿到配置文件中的数据
			}
			
			$this->connect();//实行数据库操作

			$this->table = $this->prefix.$table;//组装表名，前缀加表名
		}

		private function connect(){
			$host = $this->db_config['DB_CONF']['DB_HOST'];//读取配置数组中的数据
			$user = $this->db_config['DB_CONF']['DB_USER'];
			$passwd = $this->db_config['DB_CONF']['DB_PASSWD'];
			$port = $this->db_config['DB_CONF']['DB_PORT'];
			$database = $this->db_config['DB_CONF']['DB_DATABASE'];
			$this->prefix = $this->db_config['DB_CONF']['DB_PREFIX'];
			$charset = $this->db_config['DB_CONF']['DB_CHARSET'];

			$this->linkID = mysql_connect($host,$user,$passwd) or die("连接数据库失败".mysql_error());//
			mysql_select_db($database);//连接数据库
			mysql_query("SET NAMES UTF8");//设置编码

		}

		public function Alter($data){//修改字段
			$str = "";
			foreach($data as $k=>$v){
				$str .="$k $v;"; 
			}
			
			$this->sql = "ALTER TABLE $this->table CHANGE COLUMN $str";
			$query = mysql_query($this->sql);
			if($query){
				$this->S_log($this->sql);
			}else{
				$this->S_log($this->sql,mysql_error());
			}	
			return mysql_insert_id();
		}

		public function Add($data){//添加字段
			$str = "";
			foreach($data as $k=>$v){
				$str.="ADD $k $v,";
			}
			$str = rtrim($str,",");
			$this->sql = "ALTER TABLE $this->table $str;";
			$query = mysql_query($this->sql);
			if($query){
				$this->S_log($this->sql);
			}else{
				$this->S_log($this->sql,mysql_error());
			}
			return mysql_affected_rows();
		}


		public function Drop($data){
			$str="";
			foreach($data as $item){
				$str .="DROP $item,";
			}
			$str = rtrim($str,",");
			$this->sql = "ALTER TABLE $this->table $str;";
			$query = mysql_query($this->sql);
			if($query){
				$this->S_log($this->sql);
			}else{
				$this->S_log($this->sql,mysql_error());
			}
			return mysql_affected_rows();
		}

		/*public function select($where = 1,$count=""){//SQL查询语句
			if($count == ""){
				$this->sql = "SELECT * FROM $this->table WHERE $where";
			}else{
				$this->sql = "SELECT $count FROM $this->table WHERE $where";
			}
			//编写sql语句
			return $this;
		}*/
		public function select($where = 1,$count="",$select = ""){//SQL查询语句
			if($count == "" && $select == ""){
				$this->sql = "SELECT * FROM $this->table WHERE $where";
			}else if(!(empty($select)) && $count == ""){
				$this->sql = "SELECT * FROM $this->table $select WHERE $where";
			}else{
				$this->sql = "SELECT $count FROM $this->table WHERE $where";
			}
			// echo $this->sql;exit;
			//编写sql语句
			return $this;
		}


		public function limit($limit = array(0,1000)){
			if(count($limit) == 2){
				$this->sql .= "LIMIT $limit[0],$limit[1]";
			}else{
				$this->sql .="LIMIT $limit[0]";
			}
			return $this;
		}
		public function orderBy($name = array('','ASC')){
			$this->sql .=" ORDER BY $name[0] $name[1] ";
			return $this;

		}

		public function getOne(){//获取一条信息
			$query = mysql_query($this->sql);
			if($query){
				$this->S_log($this->sql);
			}else{
				$this->S_log($this->sql,mysql_error());
			}

			$data = array();
			while($row = mysql_fetch_assoc($query)){
				$data = $row;
			}
			return $data;//将数据return出来
		}

		public function getAll(){//获取多条数据
			$query = mysql_query($this->sql);
			if($query){
				$this->S_log($this->sql);
			}else{
				$this->S_log($this->sql,mysql_error());
			}
			
			$data = array();
			while($row = mysql_fetch_assoc($query)){
				$data[] = $row;
			}
			return $data;
		}

		public function save($data){//插入数据
			$arr = array_keys($data);
			$k = "`".implode("`,`",$arr)."`";
			$v = '"'.implode('","',$data).'"';
			$this->sql = "INSERT INTO $this->table($k) VALUES($v)";
			$query = mysql_query($this->sql);
			if($query){
				$this->S_log($this->sql);
			}else{
				$this->S_log($this->sql,mysql_error());
			}
			return mysql_insert_id();
		}

		public function update($data,$where = 1){//更新数据
			$str = "";
			foreach($data as $k=>$v){
				$str .= "$k='$v',";
			}
			$str = rtrim($str,",");
			$this->sql = "UPDATE $this->table SET $str WHERE $where";
			/*var_dump($this->sql);exit;*/
			$query = mysql_query($this->sql);
			if($query){
				$this->S_log($this->sql);
			}else{
				$this->S_log($this->sql,mysql_error());
			}
			return mysql_affected_rows();
		}

		public function delete($where = 1){//删除数据
			$this->sql = "DELETE FROM $this->table WHERE $where";
			$query = mysql_query($this->sql);
			if($query){
				$this->S_log($this->sql);
			}else{
				$this->S_log($this->sql,mysql_error());
			}
			return mysql_affected_rows();
		}

		public function S_log($sql,$error = ""){//日志
			if($error){
				$insert_sql = "[".date("Y/m/d H:i:s")."]  ".$sql."  ".$error."\r\n";
			}else{
				$insert_sql = "[".date("Y/m/d H:i:s")."]  ".$sql."\r\n";
			}
			file_put_contents("log.txt",$insert_sql,FILE_APPEND);
		}	
	}



?>
